package com.li.utils;

import android.content.Context;
import android.util.TypedValue;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Author: Li
 * Date: 2017/4/1
 * Description: 转换
 */

public class TransformUtil {
    public static int dp2px(Context context, float dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dp, context.getApplicationContext().getResources().getDisplayMetrics());
    }

    public static int sp2px(Context context, float sp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                sp, context.getApplicationContext().getResources().getDisplayMetrics());
    }

    public static String keep2points(double value) {
        DecimalFormat format = new DecimalFormat("0.00");
        return format.format(value);
    }

    public static String keep2points(String value) {
        return keep2points(Double.parseDouble(value));
    }

    /**
     * 时间戳转时间
     * @param date  时间戳
     * @return  日期字符串
     */
    public static String timeFormat(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);

        long time=Long.valueOf(date);
        return format.format(new Date(time*1000));
    }
}
